package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 更新商品信息
 */

@WebServlet(name = "UpdateProductServlet",value="/UpdateProductServlet")
public class UpdateProductServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
        request.setCharacterEncoding("utf-8");
        // 获取商品 ID
        int Id = Integer.parseInt(request.getParameter("id"));

        // 获取商品信息
        String name=request.getParameter("name");//从前端获取商品名称
        String type=request.getParameter("type");
        Double price=Double.parseDouble(request.getParameter("price"));//从前端获取商品价格
        Double number=Double.parseDouble(request.getParameter("number"));//从前端获取商品数量
        String guige=request.getParameter("guige");
        String provider=request.getParameter("provider");//从前端获取商品描述
        // 连接数据库并执行更新操作

        // 根据更新结果设置响应状态码和消息
        if (updateProductInfo(Id, name,type,price,number,guige,provider)) {
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().write("商品信息更新成功");
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("商品信息更新失败");
        }
    }

    private boolean updateProductInfo(int id, String name, String type, double price, double number, String guige, String provider) {
        // 在此处实现具体的数据库更新逻辑
        // 返回更新是否成功的结果
        return true;
    }
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
}
}